<?php
namespace  app\manage\model;
use think\Db;
use think\Model;
class PlatformAccount extends Model{
    protected  $table = 'platform_account';
    /**
     * @param $page             页码
     * @param $rows             一页的列数
     * @param $keyword          关键字
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
   public function onePage($page, $rows, $keyword) {
       $where = array(
            'name' => array('like', '%'.$keyword.'%'),
            'admin' => array('like', '%'.$keyword.'%'),
            'tel' => array('like', '%'.$keyword.'%'),
            'address' => array('like', '%'.$keyword.'%'),
            'street' => array('like', '%'.$keyword.'%')
       );
       $data=Db::name($this->table)
           ->whereOr($where)
           ->page($page, get_list_rows())
           ->field('platform_account_id,name,type,admin,tel,address,street,create_time,status')
           ->select();
       return $data;
   }

    /**
     * @param $keyword          关键字
     * @return int|string       总数
     * @throws \think\Exception
     */
    public function total($keyword) {
        $where = array(
            'name' => array('like', '%'.$keyword.'%'),
            'admin' => array('like', '%'.$keyword.'%'),
            'tel' => array('like', '%'.$keyword.'%'),
            'address' => array('like', '%'.$keyword.'%'),
            'street' => array('like', '%'.$keyword.'%')
        );
        $data=Db::name($this->table)
            ->whereOr($where)
            ->count();
        if (gettype($data) !== 'integer') die;
        return $data;
    }

    /**
     * @param $data
     * @return int|string
     */
    public function m_insert($data) {
        $data = Db::name($this->table)
            ->insert($data);
        return Db::name($this->table)->getLastInsID();
    }

    /**
     * 编辑
     * @param $data
     * @return int|string
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function m_update($platform_account_id, $data) {
        $data = Db::name($this->table)
            ->where(['platform_account_id'=>$platform_account_id])
            ->update($data);
        return $data;
    }

    /**
     * @param $data
     * @return int|string
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function m_delete($platform_account_id) {
        $data = Db::name($this->table)
            ->where(['platform_account_id'=>$platform_account_id])
            ->delete();
        return $data;
    }

    /**
     * @param $platform_account_id
     * @return array|false|\PDOStatement|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function detail($platform_account_id) {
        $data = Db::name($this->table)
            ->where(['platform_account_id'=>$platform_account_id])
            ->field('platform_account_id, name, type, address, street, admin, tel, pass, added_permission, basic_permission, status, create_time, update_time')
            ->find();
        return $data;
    }

    /**
     * @param $staff_id
     * @return array|false|\PDOStatement|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getKey($platform_account_id) {
        $data = Db::name($this->table)
            ->where(['platform_account_id'=>$platform_account_id])
            ->field('platform_account_id, key')
            ->find();
        return $data;
    }
}